package com.shop.cloud.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.dao.system.SysPositionDao;
import com.shop.cloud.dao.system.SysRoleResourceDao;
import com.shop.cloud.domain.system.SysPosition;
import com.shop.cloud.domain.system.SysRoleResource;
import com.shop.cloud.service.system.ISysRoleResourceService;

/**
 * sys_role_resource
 */
@Service("sysRoleResourceService")
public class SysRoleResourceService extends BaseService<SysRoleResource> implements ISysRoleResourceService{
    
    @Autowired
	private SysRoleResourceDao sysRoleResourceDao;
    
    @Autowired
    private SysPositionDao sysPositionDao;

	@Override
	public MybatisDao<SysRoleResource> getEntityDao() {
		return sysRoleResourceDao;
	}
    
	/**
	 * 
	 * @param 菜单修改
	 * @param gmc
	 * @param sysRoleResource 职位与权限
	 * @return
	 */
	@Override
	public int updateRoleResource(SysRoleResource sysRoleResource) {
//        
//		SysPosition sysPosition = new SysPosition();
//		sysPosition.setId(sysRoleResource.getPositionId());
		SysPosition sysPosition = sysPositionDao.get(sysRoleResource.getPositionId());
		sysPosition.setAuthority(sysRoleResource.getAuthority());
		//修改职位数据权限
		int row = sysPositionDao.update(sysPosition);
		 if(row>=0){
			//得到当前职位数据
			List<SysRoleResource> sysRoleResourceList = this.find(sysRoleResource);
				//判断当前职位是否有菜单
				if(sysRoleResourceList.size()>0){
					//删除成功
					 if(sysRoleResourceDao.removeByEntity(sysRoleResource)>0){
						 //判断用户是否勾选菜单
						 if(sysRoleResource.getResourceIds().size()>0){
							 
							 for(int i=0;i<sysRoleResource.getResourceIds().size();i++){
								 SysRoleResource sysRoleResourceTwo = new SysRoleResource();
								 sysRoleResourceTwo.setPositionId(sysRoleResource.getPositionId());
								 sysRoleResourceTwo.setResourceId(sysRoleResource.getResourceIds().get(i));
								//执行添加
								 sysRoleResourceDao.insert(sysRoleResourceTwo);
							 }
						 }
						 return 1;
					 }
				}else{
					//判断用户是否勾选菜单
					if(sysRoleResource.getResourceIds().size()>0){
						 for(int i=0;i<sysRoleResource.getResourceIds().size();i++){
							 SysRoleResource sysRoleResourceTwo = new SysRoleResource();
							 sysRoleResourceTwo.setPositionId(sysRoleResource.getPositionId());
							 sysRoleResourceTwo.setResourceId(sysRoleResource.getResourceIds().get(i));
							//执行添加
							 sysRoleResourceDao.insert(sysRoleResourceTwo);
							 
						 }
						 return 1;
					 }
						 //暂无修改
						 return 2;
				}
		    }
		    //失败
			return 0;
	}
}